package com.jcark.ui.view.magicindicator


import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import androidx.viewpager.widget.ViewPager
import com.jcark.ui.utils.ViewUtils
import net.lucode.hackware.magicindicator.MagicIndicator
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView

/**
 * 指示器工具类
 */
object MagicIndicatorHelper {

    /**
     * 渐变色指示器
     */
    fun createMagicIndicator(
        context: Context,
        mainPager: ViewPager? = null,
        magicIndicator: MagicIndicator,
        titleList: MutableList<String>,
        isFollowTouch: Boolean = true,
        isAdjustMode: Boolean = true,
        tabSize: Float = 16f,
        isPerformClick: Boolean = false,
        leftPadding: Float = 10f,
        rightPadding: Float = 10f,
        normalColor: Int = Color.parseColor("#666666"),
        selectedColor: Int = Color.parseColor("#333333"),
        isSelectTextBold: Boolean = true,
        typeface: Int? = null,
        indicatorStartColor: Int = Color.parseColor("#FFFFD22D"),//指示器开始颜色
        indicatorEndColor: Int = Color.parseColor("#FFFFBB18"),//指示器结束颜色
        indicatorHorizontalPadding: Float = 10f,
        roundRadius: Float = 100f,
        tabClickListener: OnTabClickListener? = null
    ): CommonNavigator {
        val commonNavigator = CommonNavigator(context)
        commonNavigator.isFollowTouch = isFollowTouch
        commonNavigator.isAdjustMode = isAdjustMode
        commonNavigator.leftPadding = ViewUtils.dpToPx(context, leftPadding)
        commonNavigator.rightPadding = ViewUtils.dpToPx(context, rightPadding)
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount(): Int {
                return titleList.size
            }

            @SuppressLint("NewApi")
            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val simplePagerTitleView: SimplePagerTitleView =
                    if (isSelectTextBold) ScaleSizePagerTitleView(context) else SimplePagerTitleView(context)
                simplePagerTitleView.text = titleList[index]
                simplePagerTitleView.normalColor = normalColor
                simplePagerTitleView.selectedColor = selectedColor
                simplePagerTitleView.textSize = tabSize
                typeface?.let {
                    simplePagerTitleView.typeface = Typeface.defaultFromStyle(it)
                }
                if (isPerformClick) {
                    simplePagerTitleView.performClick()
                }
                simplePagerTitleView.setOnClickListener {
                    mainPager?.let {
                        it.currentItem = index
                    }
                    tabClickListener?.onTabClick(index)
                }

                return simplePagerTitleView
            }

            @SuppressLint("NewApi")
            override fun getIndicator(context: Context): IPagerIndicator {
                val indicator = CustomWrapPagerIndicator(context)
                indicator.setFillColors(indicatorStartColor, indicatorEndColor)
                indicator.roundRadius = roundRadius
                indicator.horizontalPadding = ViewUtils.dpToPx(context, indicatorHorizontalPadding)
                return indicator
            }
        }
        magicIndicator.navigator = commonNavigator
        if (null != mainPager) {
            ViewPagerHelper.bind(magicIndicator, mainPager)
        }

        return commonNavigator
    }
}

interface OnTabClickListener {
    fun onTabClick(position: Int)
}
